cmake_minimum_required(VERSION 3.16)
project(vimlc VERSION 0.1.0 LANGUAGES CXX)

# 推荐使用Ninja构建系统以获得更快的构建速度
# cmake -G Ninja .. && ninja

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# 编译选项
if(MSVC)
    add_compile_options(/W4)
else()
    add_compile_options(-Wall -Wextra -Wpedantic)
endif()
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# 设置构建类型
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Debug)
endif()

# 设置输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

# 包含头文件目录
include_directories(include)

# 添加GoogleTest
add_subdirectory(thirdparty/googletest)

# 收集源文件
file(GLOB_RECURSE VIML_SOURCES "src/viml/*.cpp")
file(GLOB_RECURSE VIML_HEADERS "include/viml/*.h")


# 排除测试文件
list(REMOVE_ITEM VIML_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/test/*.cpp")

# 创建VimL虚拟机核心库
add_library(vimlc_core ${VIML_SOURCES} ${VIML_HEADERS})
target_include_directories(vimlc_core PUBLIC include)

# 创建主执行程序
add_executable(vimlc src/main.cpp)
target_link_libraries(vimlc vimlc_core)

# 创建示例程序
add_executable(scope_basic_test test/scope_basic_test.cpp)
target_link_libraries(scope_basic_test vimlc_core gtest gtest_main)
# add_executable(scope_function_test test/scope_function_test.cpp)
# target_link_libraries(scope_function_test vimlc_core gtest gtest_main)

# 创建分代垃圾回收器测试
add_executable(generational_gc_test test/generational_gc_simple_test.cpp)
target_link_libraries(generational_gc_test vimlc_core gtest gtest_main)

# 创建调试器测试
add_executable(debugger_test test/debugger_test.cpp)
target_link_libraries(debugger_test vimlc_core)

# 创建CLI调试器测试
add_executable(cli_debugger_test test/cli_debugger_test.cpp)
target_link_libraries(cli_debugger_test vimlc_core)

# 创建优化器测试
add_executable(optimizer_test test/optimizer_test.cpp)
target_link_libraries(optimizer_test vimlc_core gtest gtest_main)

# 创建标准库测试
add_executable(builtin_file_test test/builtin_file_test.cpp)
target_link_libraries(builtin_file_test vimlc_core gtest gtest_main)

add_executable(builtin_path_test test/builtin_path_test.cpp)
target_link_libraries(builtin_path_test vimlc_core gtest gtest_main)

add_executable(builtin_sys_test test/builtin_sys_test.cpp)
target_link_libraries(builtin_sys_test vimlc_core gtest gtest_main)

# 创建独立CLI调试器
add_executable(vimlc_debug src/vimlc_debug.cpp)
target_link_libraries(vimlc_debug vimlc_core)

# 设置默认目标
add_custom_target(vimlc_debug_by_name
    COMMAND vimlc_debug src/vimlc_debug.cpp
    DEPENDS vimlc_debug)

# 启用测试
enable_testing()
